<?php
    require('init.php');
    require('header.php');
    
    $client = Client::rechercheClient(Tools::getValue('id', 'GET'));
    $civilites = Civilite::getCivilites();
    if(Tools::getValue('ajout')) {
        $client = new Client(0, Civilite::rechercheCivilite(Tools::getValue('id_civilite')), Profil::rechercheProfil(1), 
                            Tools::getValue('login'), Tools::getValue('mdp'), Tools::getValue('nom'), Tools::getValue('prenom'), 
                            Tools::getValue('telephone'), Tools::getValue('mobile'), Tools::getValue('email'), 
                            Tools::getValue('date_naissance'), new DateTime(), new DateTime(), Tools::getValue('valide'));
        $client->add();
    }
    else if(Tools::getValue('modif')) {
        $client->setCivilite(Civilite::rechercheCivilite(Tools::getValue('id_civilite')));
        $client->setLogin(Tools::getValue('login'));
        $client->setNom(Tools::getValue('nom'));
        $client->setPrenom(Tools::getValue('prenom'));
        $client->setTelephone(Tools::getValue('telephone'));
        $client->setMobile(Tools::getValue('mobile'));
        $client->setEmail(Tools::getValue('email'));
        $client->setDateNaissance(Tools::getValue('date_naissance'));
        $client->setValide(Tools::getValue('valide'));
        $client->update();
    }
    
    if(!$client instanceof Client) {
        echo    '<form method="POST" action="details_client.php">
                    <div class="container_12 formulaire">
                        <h2>
                            Ajout d\'un client
                        </h2>
                        <div class="grid_1">
                            &nbsp;
                        </div>
                        <div class="grid_3 label_admin">
                            Civilité<br>
                            Identifiant<br>  
                            Mot de passe<br>    
                            Prénom<br>
                            Nom<br>
                            Téléphone<br>
                            Mobile<br>
                            Email<br>
                            Date de naissance<br>
                            Valide<br>
                        </div>
                        <div class="grid_6 formulaire_admin">
                            <select name="id_civilite">';
        foreach($civilites as $civilite) {
            echo                '<option value="'.$civilite->getId().'">'.$civilite.'</option>';
        }
        echo                '</select>
                            <input type="text" name="login" /><br>
                            <input type="password" name="mdp" /><br>
                            <input type="text" name="prenom" /><br>
                            <input type="text" name="nom" /><br>
                            <input type="text" name="telephone" /><br>
                            <input type="text" name="mobile" /><br>
                            <input type="text" name="email" /><br>
                            <input type="text" placeholder="dd/mm/YYYY" name="date_naissance" /><br>
                            <input type="checkbox" name="valide" value="1" CHECKED /><br>
                            <input type="submit" class="btn" value="Ajouter" name="ajout" />
                        </div>
                    </div>
                </form>';
    }
    else {
        $client_checked = "";
        if($client->getValide() == 1) {
            $client_checked = "CHECKED";
        }
        echo    '<form method="POST" action="details_client.php?id='.$client->getId().'">
                    <div class="container_12 formulaire">
                        <h2>
                            Modification du compte client de '.$client.'
                        </h2>
                        <div class="grid_1">
                            &nbsp;
                        </div>
                        <div class="grid_3 label_admin">
                            Civilité<br>
                            Identifiant<br>  
                            Prénom<br>
                            Nom<br>
                            Téléphone<br>
                            Mobile<br>
                            Email<br>
                            Date de naissance<br>
                            Valide<br>
                        </div>
                        <div class="grid_6 formulaire_admin">
                            <select name="id_civilite">
                                <option value="'.$client->getCivilite()->getId().'">'.$client->getCivilite().'</option>';
        foreach($civilites as $civilite) {
            echo                '<option value="'.$civilite->getId().'">'.$civilite.'</option>';
        }
        echo                '</select>
                            <input type="text" name="login" value="'.$client->getLogin().'"/><br>
                            <input type="text" name="prenom" value="'.$client->getPrenom().'"/><br>
                            <input type="text" name="nom" value="'.$client->getNom().'"/><br>
                            <input type="text" name="telephone" value="'.$client->getTelephone().'"/><br>
                            <input type="text" name="mobile" value="'.$client->getMobile().'"/><br>
                            <input type="text" name="email" value="'.$client->getEmail().'"/><br>
                            <input type="text" placeholder="dd/mm/YYYY" name="date_naissance" value="'.$client->getDateNaissance('d/m/Y').'"/><br>
                            <input type="checkbox" name="valide" value="1" '.$client_checked.' /><br>
                            <input type="submit" class="btn" value="Modifier" name="modif" />
                        </div>
                    </div>
                </form>';
    }
?>
